package com.cl.ks.service;

import com.cl.ks.entity.TaskList;
import com.cl.ks.entity.TaskListItem;
import com.cl.ks.repository.TaskListItemRepository;
import com.cl.ks.repository.TaskListRepository;
import com.cl.ks.utils.DbUtil;
import com.cl.ks.utils.TaskServiceUtil;
import com.cl.pile.rule.process.RuleMatcher;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.cl.ks.constant.QTableConstant.qTaskList;
import static com.cl.ks.constant.QTableConstant.qTaskListItem;

/**
 * @author chenyitao
 * @date 2021/4/20
 */
@Service
public class TaskListService {


    private final JPAQueryFactory queryFactory;
    private final TaskListRepository taskListRepository;
    private final TaskListItemRepository taskListItemRepository;
    private final TaskServiceUtil taskServiceUtil;

    private final List<String> taskTypeList = new ArrayList<>();

    public static final String COMPARE_TASK = "比对任务";
    public static final String FILTER_TASK = "提取任务";

    public TaskListService(RuleMatcher ruleMatcher, DbUtil dbUtil, JPAQueryFactory queryFactory, TaskListRepository taskListRepository, TaskListItemRepository taskListItemRepository, TaskServiceUtil taskServiceUtil) {
        this.queryFactory = queryFactory;
        this.taskListRepository = taskListRepository;
        this.taskListItemRepository = taskListItemRepository;
        this.taskServiceUtil = taskServiceUtil;
        taskTypeList.add(COMPARE_TASK);
        taskTypeList.add(FILTER_TASK);
    }

    @Data
    public static class PageParam {
        Integer page;
        Integer size;
    }

//    @Data
//    public static class TaskList {
//        Integer id;
//        String name;
//    }

//    @Data
//    public static class TaskListItem {
//        Integer id;
//        Integer taskListId;
//        Integer taskId;
//        String taskType;
//        Integer sortNumber;
//    }


    public ResponseEntity getProgress(Integer taskListId) {
        return taskServiceUtil.getProgress(taskListId);
    }
    public ResponseEntity<Object> executeTaskList(Integer taskListId) {
        return taskServiceUtil.executeTaskList(taskListId);
    }

    public ResponseEntity<Object> listTaskType() {
        return ResponseEntity.ok(taskTypeList);
    }


    public ResponseEntity<Object> pageTaskList(PageParam param) {
        List<TaskList> taskLists = queryFactory.selectFrom(qTaskList).offset((long) (param.page - 1) * param.size).limit(param.size).fetch();
        return ResponseEntity.ok().body(taskLists);
    }

    public ResponseEntity<Object> listTaskListItem(Integer taskListId) {
        List<TaskListItem> taskListItems = queryFactory.selectFrom(qTaskListItem).where(qTaskListItem.taskListId.eq(taskListId)).orderBy(qTaskListItem.sortNumber.asc()).fetch();
        return ResponseEntity.ok().body(taskListItems);
    }

    public ResponseEntity<Object> addTaskList(TaskList taskList) {
        return ResponseEntity.ok().body(taskListRepository.save(taskList));
    }

    public ResponseEntity<Object> addTaskListItem(TaskListItem taskListItem) {

        return ResponseEntity.ok().body(taskListItemRepository.save(taskListItem));
    }

    public ResponseEntity<Object> editTaskList(TaskList taskList) {
        return ResponseEntity.ok().body(taskListRepository.save(taskList));
    }

    public ResponseEntity<Object> editTaskListItem(TaskListItem taskListItem) {
        return ResponseEntity.ok().body(taskListItemRepository.save(taskListItem));
    }

}
